<template>
  <!--用户信息管理页 -->
  <div class="main">
    <!-- 用户信息列表渲染 -->
    <el-table
      :data="users"
      style="width: 1002px; margin: 0 auto"
      max-height="352"
      stripe
      border
      :header-cell-style="headerStyle"
    >
      <!-- ID项 -->
      <el-table-column prop="userId" label="ID" width="300"> </el-table-column>
      <!-- 名称项 -->
      <el-table-column prop="username" label="名称" width="300">
      </el-table-column>
      <!-- 操作栏 -->
      <el-table-column label="操作" width="400">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            :disabled="scope.$index == 0"
            type="primary"
            @click="edit(scope.$index)"
            icon="el-icon-edit"
            size="small"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            :disabled="scope.$index == 0"
            @click="deleteRow(scope.$index, users)"
            size="small"
            type="primary"
            icon="el-icon-delete"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加分类按钮 -->
    <el-button
      @click="addDialogFlag = true"
      class="addButton"
      size="medium"
      type="primary"
      round
      style="margin-bottom: 10px"
      >添加用户</el-button
    >
    <!-- 添加提示输入名称dialog -->
    <el-dialog
      title="请输入必要信息："
      :visible.sync="addDialogFlag"
      width="30%"
      center
      top="30vh"
    >
      用户名：<el-input v-model="addUser.user" size="small"> </el-input>
      密码：<el-input v-model="addUser.password" size="small" type="password"> </el-input>
      电话号码：<el-input v-model="addUser.phone" size="small"> </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑信息 -->
    <el-dialog
      title="请编辑用户信息："
      :visible.sync="editDialogFlag"
      width="30%"
      center
      top="30vh"
    >
      用户名：<el-input v-model="editUser.username" size="small"> </el-input>
      电话号码：<el-input v-model="editUser.phone" size="small"> </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      users: [],
      addUser: {},
      editUser:{},
      addDialogFlag: false,
      editDialogFlag: false
    };
  },
  mounted() {
    this.getUsers();
  },
  methods: {
    headerStyle() {
      return "color: #000;background-color: #c5464a;height:60px;";
    },
    getUsers() {
      this.$http.get("user/list").then((response) => {
        this.users = response.data.result;
      });
    },
    deleteRow(index, data) {
      const id = data[index].userId;
      const name = data[index].username;
      this.$confirm(
        "此操作将永久删除ID为【" +
          id +
          "】，名称为【" +
          name +
          "】的项, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showCancelButton: true,
          type: "warning",
        }
      )
        .then(() => {
          data.splice(index, 1);
          this.users = data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 删除数据库中的对应项
          this.$http
            .post("user/delete", {id:id})
            .then((response) => {
              this.getUsers();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    add() {
      this.addUser.type = 0;
      this.$http.post("user/add",this.addUser).then((response) => {
        if (response.data.code != 200) {
          this.$message.error(response.data.message);
        } else {
          this.$message.success("添加成功");
          this.getUsers();
        }
      });
      this.addDialogFlag = false;
      this.addUser = {};
    },
    edit(index){
        this.editUser = this.users[index];
        this.editDialogFlag = true;
    },
    editSubmit(){

    }
  },
};
</script>

<style scope>
.main {
  overflow: hidden;
}
.el-input__inner {
  padding: 0 !important;
}
.addButton {
  margin-top: 12px !important;
  float: right;
  right: 30px;
}
</style>
